﻿using Autofac;
using Infrastructure.Job;
using System;
using System.Collections.Generic;
using System.Text;

namespace Infrastructure
{
    public static class ServiceProvide
    {
        private static ILifetimeScope LifetimeScope { get; set; }


        public static void SetAutofac(ILifetimeScope lifetimeScope)
        {
            LifetimeScope = lifetimeScope;
        }


        public static void DoLifeScope(Action<ILifetimeScope> action)
        {
            using (var scope= LifetimeScope.BeginLifetimeScope())
            {
                action(scope);
            }
        }
        public static T DoLifeScope<T>(Func<ILifetimeScope,T> fun)
        {
            using (var scope = LifetimeScope.BeginLifetimeScope())
            {
                return fun(scope);
            }
        }
        public static T Build<T>(Type type)
        {
            var instance = Activator.CreateInstance(type);

            return (T)LifetimeScope.InjectProperties(instance);
        }
        public static T Build<T>()
        {
            var instance = Activator.CreateInstance<T>();

            return LifetimeScope.InjectProperties(instance);
        }

        public static T Get<T>()
        {
            return LifetimeScope.Resolve<T>();
        }
    }
}
